public class Solution31 {

    public static void nextPermutation(int[] nums){
        int i = nums.length - 2;
        while (i>=0 && nums[i+1]<=nums[i]){
            i--;
        }
        if (i>=0){
            int j = nums.length-1;
            while (j>=0 && nums[j]<=nums[i]){
                j--;
            }
            swap(nums,i,j);
        }
        reverse(nums,i+1);
    }
    public static void reverse(int[]arr, int start){
        int end = arr.length-1;
        while (start<end){
            swap(arr, start, end);
            start ++;
            end --;
        }

    }
    public static void swap(int[]arr, int i, int j){
        int k = arr[i];
        arr[i] = arr[j];
        arr[j] = k;
    }
}
